﻿using ProTrack.Entity;
using ProTrack.IRepository;
using ProTrack.IServices.Examine;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace ProTrack.Services.Examine
{
    public class QcProjectLevelServices : IQcProjectLevelServices
    {


        //异步方法
        public QcProjectLevelServices(IBaseRepository db)
        {
            Db = db;
        }

        public IBaseRepository Db { get; }

        /// <summary>
        /// 插入单条数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<bool> AddAsync(QcProjectLevel model)
        {
            var result = await Db.InsertAsync(model);
            return result;
        }


        /// <summary>
        /// 批量删除数据
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(int[] Ids)
        {
            string sql = "delete QcProjectLevel where Id in @Ids";
            var result = await Db.DeleteAsync(sql, new { Ids });
            return result;
        }


        /// <summary>
        /// 删除单条数据
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(int Id)
        {
            string sql = "delete QcProjectLevel where Id = @Id";
            var result = await Db.DeleteAsync(sql, new { Id });
            return result;
        }


        /// <summary>
        /// 获取所有数据
        /// </summary>
        /// <returns></returns>
        public async Task<IEnumerable<QcProjectLevel>> GetAllAsync()
        {
            string sql = "select * from QcProjectLevel";
            var list = await Db.QueryAllAsync<QcProjectLevel>(sql);
            return list;
        }


        /// <summary>
        /// 获取分页  根据条件查询数据
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="Remark"></param>
        /// <param name="LevelName"></param>
        /// <param name="Classify"></param>
        /// <returns></returns>
        public async Task<IEnumerable<QcProjectLevel>> GetAsync(int PageIndex, int PageSize, string Remark, string LevelName, string Classify)
            {
            string sql = "select * from QcProjectLevel where 1=1 ";
            if (!string.IsNullOrEmpty(Remark))
            {
                sql += " and Remark like @Remark ";
            }

            if (!string.IsNullOrEmpty(LevelName))
            {
                sql += " and LevelName like @LevelName ";
            }

            if (!string.IsNullOrEmpty(Classify))
            {
                sql += " and Classify like @Classify ";
            }

            sql += " order by CreateTime desc offset @PageSize*(@PageIndex-1) row fetch next @PageSize rows only ";


            var list = await Db.QueryAsync<QcProjectLevel>(sql, new { PageIndex, PageSize, Remark = $"%{Remark}%", LevelName=$"{LevelName}", Classify = $"%{Classify}%" });
            return list;
        }


        /// <summary>
        /// 获取总条数
        /// </summary>
        /// <param name="Remark"></param>
        /// <param name="LevelName"></param>
        /// <param name="Classify"></param>
        /// <returns></returns>
        public async Task<int> GetCountAsync(string Remark, string LevelName, string Classify)
        {
            string sql = "select count(*) from QcProjectLevel where 1=1 ";
            if (!string.IsNullOrEmpty(Remark))
            {
                sql += " and Remark like @Remark ";
            }

            if (!string.IsNullOrEmpty(LevelName))
            {
                sql += " and LevelName like @LevelName ";
            }

            if (!string.IsNullOrEmpty(Classify))
            {
                sql += " and Classify like @Classify ";
            }

            var count = await Db.GetRecoreCountAsync(sql, new { Remark = $"%{Remark}%", LevelName = $"%{LevelName}%" , Classify =$"{Classify}"});
            return count;
        }


        /// <summary>
        /// 事物修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<bool> UpdateAsync(QcProjectLevel model)
        {
            var result = await Db.UpdateAsync(model);
            return result;
        }
    }
}
